import request from '../../utils/request'
import {requestString} from '../../utils/request'
import {download} from '../../utils/commons'

/**
 * 分页获取开票资料
 * @param contractno
 * @param userIds
 * @param openinvoicestatus
 * @param page
 * @returns {Promise.<void>}
 */
export async function getInvoicesList({contractno, userIds, openinvoicestatus, page}) {
    /**
     * TODO
     * @type {number}
     */
    userIds = 1;
    return request(apiPath.mc + "/api/tax/invoices", {
        method: 'get',
        data: {contractno, userIds, openinvoicestatus, page: page.page, size: page.size}
    });
}

/**
 * 预开票
 * @param orderId
 * @param preamount
 * @param prerate
 * @param precharge
 * @param amountcurrency
 * @param chargecurrency
 * @returns {Promise.<void>}
 */
export async function preinvoiceopen({orderId, preamount, prerate, precharge, amountcurrency, chargecurrency}) {
    return request(apiPath.mc + "/api/tax/preinvoiceopen", {
        method: 'post',
        data: {orderId, preamount, prerate, precharge, amountcurrency, chargecurrency}
    });
}

/**
 * 更新预开票详情
 */
export async function updatePreInvoice(data) {
    return request(apiPath.mc + "/api/tax/preInvoice", {
        method: 'put',
        data
    });
}


/**
 * 根据订单获取开票资料表头信息
 *
 * @param orderId
 * @returns {Promise.<void>}
 */
export async function inoviceheader({orderId}) {
    return request(apiPath.mc + "/api/tax/inoviceheader", {
        method: 'get',
        data: {orderId}
    });

}


/**
 * 根据订单获取开票资料具体产品信息
 *
 * @param orderId
 * @returns {Promise.<void>}
 */
export async function inoviceproducts({orderId}) {
    return request(apiPath.mc + "/api/tax/inoviceproducts", {
        method: 'get',
        data: {orderId}
    });

}

/**
 * 根据订单号获取预开票信息
 */
export async function taxpreopeninvoice({orderId}) {
    return request(apiPath.mc + "/api/tax/preinvoiceopen/" + orderId, {
        method: 'get',
        data: {orderId}
    });

}
/**
 * 查询开票人
 *
 * @param cstId
 * @param status
 * @returns {Promise.<Object>}
 */
export async function queryDrawers({ cstId, status }) {
    return request(apiPath.mc + "/api/drawers/all", {
        method: 'GET',
        data: { cstId, status }
    });
}

/**
 * 导出excel
 * @param orderId
 * @returns {Promise.<void>}
 */
export async function exportInvoice({orderId,drawerId}) {
    let url =  apiPath.mc + "/api/exportInvoice";
    download(url, {orderId: orderId, filename: '开票资料'/*,drawerId:drawerId*/});

}

/**
 *
 * @param form
 * @returns {Promise.<*>}
 */
export async function openinvoice({form }) {
    return requestString(apiPath.mc + "/api/tax/openinvoice", {
        method: 'PUT',
        data: {...form}
    });
}

/**
 * 更新开票方式
 * @param orderId
 * @param invoiceType
 * @returns {*}
 */
export async function updateInvoiceType({orderId, invoiceType}) {
    return request(apiPath.mc + "/api/tax/invoicetype", {
        method: 'PUT',
        data: {orderId, invoiceType }
    });
}


/**
 * 导出开票资料列表
 * @param contractno
 * @param openinvoicestatus
 * @returns {Promise<void>}
 */
export async function exportInvoiceList({contractno,openinvoicestatus}) {
    let url =  apiPath.mc + "/api/report/taxinv/invoices/export";

    download(url, {contractno, openinvoicestatus, filename: '订单开票资料信息表'});

}

/**
 * 取消预开票
 */
export async function cancelPreInvoice(data) {
    return request(apiPath.mc + "/api/tax/cancelPreInvoice", {
        method: 'put',
        data
    });
}